/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: Buff.java 40 2008-05-10 11:56:51Z jens464 $
*/
package albareth.core.buff;

import albareth.core.Game;
import albareth.core.entity.Mobile;
import albareth.frontend.Tile;
import albareth.util.MobileTask;

public abstract class Buff extends MobileTask
{
    private String _name;
    private Tile _tile;
    private long _period;
    private int _ticks;
    private int _maxStack;
    private int _stack = 0;
    private boolean _beneficial = false;
    
    public Buff(String name, Tile tile, long period)
    {
        this(name, tile, period, 0, 0);
    }
    
    public Buff(String name, Tile tile, long period, int ticks)
    {
        this(name, tile, period, ticks, 0);
    }
    
    public Buff(String name, Tile tile, long period, int ticks, int maxStack)
    {
        _name = name;
        _tile = tile;
        _period = period;
        _ticks = ticks;
        _maxStack = maxStack;
    }
    
    public long getPeriod()
    {
        return _period;
    }

    public void setPeriod(int period)
    {
        _period = period;
    }

    public final int getTicksLeft()
    {
        return _ticks;
    }

    public int getMaxStack()
    {
        return _maxStack;
    }

    public int getStack()
    {
        return _stack;
    }

    public final void increaseStack()
    {
        _stack++;
    }

    public final long getTimeToTick()
    {
        return getNextExecutionTime() - Game.getContext().getPreciseTime();
    }
    
    public final boolean initiateMobileTask(Mobile src)
    {
        onApply(src);
        return true;
    }

    public final boolean executeMobileTask(Mobile src)
    {
        onTick(src);
        _ticks--;
        if (_ticks < 0) {
            src.fadeBuff(this);
        }
        
        return true;
    }
    
    protected final void abortMobileTask(Mobile src)
    {
        onRemove(src);
    }
    
    public Tile getTile()
    {
        return _tile;
    }
    
    public String getName()
    {
        return _name;
    }
    
    protected abstract void onApply(Mobile src);
    
    protected abstract void onTick(Mobile src);
    
    protected abstract void onRemove(Mobile src);
    
    public boolean isBeneficial()
    {
        return _beneficial;
    }
    
    public void setBeneficial(boolean b)
    {
        _beneficial = b;
    }
    
    public boolean isImmune(Mobile buffTarget)
    {
        return false;
    }


}
